package worldutils.huntgame.Utils;

import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import worldutils.huntgame.HuntGame;

import java.util.Timer;
import java.util.TimerTask;

public class TimerUtils {

    private static JavaPlugin plugin;

    public static void initialize(JavaPlugin pl) {
        plugin = pl;
    }



    public static Timer timer;
    public static int elapsedTime;



    public TimerUtils() {
        this.timer = new Timer();
        this.elapsedTime = 0;
    }

    public static void startgame() {

        if (worldutils.huntgame.AutoStart.Timer.getStats()){
            worldutils.huntgame.AutoStart.Timer.stoptick();
        }

        HuntGame.Gamestatus = true;
        timer = new Timer();
        elapsedTime = 0;
        HuntGame.HunterCantMove = true;
        PlayerUtils.setGamemodetoTag(HuntGame.getMessage("spectator"), GameMode.SPECTATOR);
        PlayerUtils.setGamemodetoTag(HuntGame.getMessage("runner"), GameMode.SURVIVAL);
        PlayerUtils.setGamemodetoTag(HuntGame.getMessage("hunter"), GameMode.SURVIVAL);
        PlayerUtils.SendCompass();
        CompassUpdater.startCompassUpdater();
        CompassUpdater.startCompassNotificationTask();
        PlayerUtils.TeleportToTaggedPlayers(HuntGame.getMessage("runner"),HuntGame.TaoshengSpawn);
        PlayerUtils.TeleportToTaggedPlayers(HuntGame.getMessage("hunter"),HuntGame.ZhuishaSpawn);
        PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("runner"),HuntGame.getMessage("title-run"));
        PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("hunter"),HuntGame.getMessage("hunter-move-time").replace("*i",String.valueOf(HuntGame.runTime)));

        // 创建一个任务，每秒执行一次
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                elapsedTime++;
                if (elapsedTime <= HuntGame.runTime) {
                    PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("hunter"), HuntGame.getMessage("hunter-start-title").replace("*i", String.valueOf(HuntGame.runTime - elapsedTime)));
                  }

                // 在第HuntGame.runTime秒时触发事件
                if (elapsedTime == HuntGame.runTime) {
                    // 你可以在这里加入更多的自定义逻辑
                    HuntGame.HunterCantMove = false;
                    PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("hunter"),HuntGame.getMessage("hunter-start"));
                    PlayerUtils.sendMessageToTaggedPlayers(HuntGame.getMessage("runner"),HuntGame.getMessage("hunter-can-move"));
//                    PlayerUtils.setGamemodetoTag(HuntGame.getMessage("hunter"), GameMode.SURVIVAL);
                }
            }
        }, 0, 1000); // 0表示立即启动，1000表示每秒（1000毫秒）执行一次
    }

    public static void stopgame(String reason,String winteam) {
        if (timer != null) {
            timer.cancel(); // 取消当前计时器任务
            timer.purge(); // 清理取消的任务
            PlayerUtils.sendMessageToAllPlayer(ChatColor.YELLOW + HuntGame.getMessage("game-over"));
            PlayerUtils.sendMessageToAllPlayer(ChatColor.YELLOW + HuntGame.getMessage("over-cause") + reason);
            PlayerUtils.setAllPlayersToSpectatorAndPlaySound();
            CompassUpdater.stopCompassUpdater();
            CompassUpdater.stopCompassNotificationTask();
            if (winteam.equalsIgnoreCase(HuntGame.getMessage("runner"))){
                PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("runner"),ChatColor.GOLD + HuntGame.getMessage("win-title"));
                PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("hunter"),ChatColor.DARK_GRAY + HuntGame.getMessage("fail-title"));
                PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("spectator"),ChatColor.GOLD + HuntGame.getMessage("game-over-spectator-title").replace("*t",HuntGame.getMessage("runner")));
                PlayerUtils.sendMessageToAllPlayer(ChatColor.YELLOW +HuntGame.getMessage("game-over-message").replace("*t",HuntGame.getMessage("runner")));
            } else if(winteam.equalsIgnoreCase(HuntGame.getMessage("hunter"))){
                PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("hunter"),ChatColor.GOLD + HuntGame.getMessage("win-title"));
                PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("runner"),ChatColor.DARK_GRAY +HuntGame.getMessage("fail-title"));
                PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("spectator"),ChatColor.GOLD + HuntGame.getMessage("game-over-spectator-title").replace("*t",HuntGame.getMessage("hunter")));
                PlayerUtils.sendMessageToAllPlayer(ChatColor.YELLOW +HuntGame.getMessage("game-over-message").replace("*t",HuntGame.getMessage("hunter")));
            } else if(winteam.equalsIgnoreCase(HuntGame.getMessage("draw"))){
                PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("hunter"),ChatColor.GOLD + HuntGame.getMessage("draw"));
                PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("runner"),ChatColor.GOLD + HuntGame.getMessage("draw"));
                PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("spectator"),ChatColor.GOLD + HuntGame.getMessage("draw"));
                PlayerUtils.sendMessageToAllPlayer(ChatColor.YELLOW + HuntGame.getMessage("game-over-non-win"));
            }

            new BukkitRunnable() {
                @Override
                public void run() {
                    ReloadWorldUtils.kickAllPlayers();
                }
            }.runTaskLater(plugin, 100L);
        }
        elapsedTime = 0;
    }
}
